var express = require('express');
var bodyParser = require('body-parser')
var whiteList = require('./accessfile/accessDomin.js')
var cors = require('cors');
var multiparty = require('multiparty');
var fse = require('fs-extra');
var app = express();

app.use(cors())

console.log(whiteList);

app.use(express.static(__dirname + '/public'));

app.use(bodyParser.urlencoded({
	extended: true
}));

app.use(bodyParser.json());


/**
 * @api {post} upload 文件上传
 * @apiDescrption 文件上传
 * @apiName uplod
 * @apiGroup Upload
 * @apiParam {string} loginName 用户名
 * @apiParam {string} loginPass 密码
 *  @apiSuccessExample {json} 成功返回示例:
 *  {
 *      "success" : "true",
 *      "result" : {
 *          "name" : "loginName",
 *          "password" : "loginPass"
 *      }
 *  }
 * @apiSuccess {json} result   加油
 * @apiSuccessExample {json} 成功返回示例:
 *  {
 *      "success" : "true",
 *      "result" : {
 *          "name" : "loginName",
 *          "password" : "loginPass"
 *      }
 *  }
 * @apiSampleRequest http://localhost:3000/upload
 * @apiVersion 1.1.0
 */



app.post('/upload', function(req, res) {

	res.header("Access-Control-Allow-Headers", "X-Requested-With");
	res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
	res.header("x-powered-by", "wocao")

	console.log("白名单：", whiteList.includes(req.headers.origin));

	// req.get(headers.origin)
	if (whiteList.includes(req.headers.origin) == true) {
		res.header("Access-Control-Allow-Origin", req.headers.origin);
		res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
		console.log(req.headers.origin);
		const form = new multiparty.Form({
			uploadDir: 'temp',
		});
		// console.log(form);
		form.parse(req)

		form.on('file', function() {
			// console.log(1111111);
			console.log();
		})

		// 解决跨域

		res.send('上传成功')
		return
	} else {
		res.status(403).send({
			error: '无权限，需要开通相关域名'
		})
	}








})


app.listen(3000);
console.log('127.0.0.1:3000')
